<template>
  <div id="aCoursesList" class="bg-fa of">
    <!-- /课程详情 开始 -->
    <section class="container">
      <section class="path-wrap txtOf hLh30">
        <a href="#" title class="c-999 fsize14">首页</a>
        \
        <a href="#" title class="c-999 fsize14">{{ courseWebVo.subjectLevelOne }}</a>
        \
        <span class="c-333 fsize14">{{ courseWebVo.subjectLevelTwo }}</span>
      </section>
      <div>
        <article class="c-v-pic-wrap" style="height: 357px;">
          <section class="p-h-video-box" id="videoPlay">
            <img height="357px" :src="courseWebVo.cover" :alt="courseWebVo.title" class="dis c-v-pic">
          </section>
        </article>
        <aside class="c-attr-wrap">
          <section class="ml20 mr15">
            <h2 class="hLh30 txtOf mt15">
              <span class="c-fff fsize24">{{ courseWebVo.title }}</span>
            </h2>
            <section class="c-attr-jg">
              <span class="c-fff">价格：</span>
              <b class="c-yellow" style="font-size:24px;">￥{{ courseWebVo.price }}</b>
            </section>
            <section class="c-attr-mt c-attr-undis">
              <span class="c-fff fsize14">主讲： {{ courseWebVo.teacherName }}&nbsp;&nbsp;&nbsp;</span>
            </section>
            <section class="c-attr-mt of">
              <span class="ml10 vam">
                <em class="icon18 scIcon"></em>
                <a class="c-fff vam" title="收藏" href="#">收藏</a>
              </span>
            </section>
            <section class="c-attr-mt" v-if="isbuy || Number(courseWebVo.price) === 0">
              <a @click="pushToDetail" title="立即学习" class="comm-btn c-btn-3">立即学习</a>
            </section>
            <section class="c-attr-mt" v-else>
              <a @click="createOrders()" href="#" title="立即购买" class="comm-btn c-btn-3">立即购买</a>
            </section>
          </section>
        </aside>
        <aside class="thr-attr-box">
          <ol class="thr-attr-ol">
            <li>
              <p>&nbsp;</p>
              <aside>
                <span class="c-fff f-fM">购买数</span>
                <br>
                <h6 class="c-fff f-fM mt10">{{ courseWebVo.buyCount }}</h6>
              </aside>
            </li>
            <li>
              <p>&nbsp;</p>
              <aside>
                <span class="c-fff f-fM">课时数</span>
                <br>
                <h6 class="c-fff f-fM mt10">{{ courseWebVo.lessonNum }}</h6>
              </aside>
            </li>
            <li>
              <p>&nbsp;</p>
              <aside>
                <span class="c-fff f-fM">浏览数</span>
                <br>
                <h6 class="c-fff f-fM mt10">{{ courseWebVo.viewCount }}</h6>
              </aside>
            </li>
          </ol>
        </aside>
        <div class="clear"></div>
      </div>
      <!-- /课程封面介绍 -->
      <div class="mt20 c-infor-box">
        <article class="fl col-7">
          <section class="mr30">
            <div class="i-box">
              <div>
                <section id="c-i-tabTitle" class="c-infor-tabTitle c-tab-title">
                  <a name="c-i" class="current" title="课程详情">课程详情</a>
                </section>
              </div>
              <article class="ml10 mr10 pt20">
                <div>
                  <h6 class="c-i-content c-infor-title">
                    <span>课程介绍</span>
                  </h6>
                  <div class="course-txt-body-wrap">
                    <section class="course-txt-body">
                      <!-- <p v-html="courseWebVo.description">{{courseWebVo.description}}</p> -->
                      <p v-html="courseWebVo.description"></p>
                    </section>
                  </div>
                </div>
                <!-- /课程介绍 -->
                <div class="mt50">
                  <h6 class="c-g-content c-infor-title">
                    <span>课程大纲</span>
                  </h6>
                  <section class="mt20">
                    <div class="lh-menu-wrap">
                      <menu id="lh-menu" class="lh-menu mt10 mr10">
                        <ul>
                          <!-- 文件目录 -->
                          <li class="lh-menu-stair" v-for="chapter in chapterVideoList" :key="chapter.id">
                            <a href="javascript: void(0)" :title="chapter.title" class="current-1">
                              <em class="lh-menu-i-1 icon18 mr10"></em>{{ chapter.title }}
                            </a>

                            <ol class="lh-menu-ol" style="display: block;">
                              <li class="lh-menu-second ml30" v-for="video in chapter.children" :key="video.id">
                                <!-- <a :href="'/player/'+video.videoSourceId" target="_blank"> -->
                                <a target="_blank" @click="watchVideo(video)" style="cursor:pointer">
                                  <span class="fr">
                                    <i class="free-icon vam mr10" v-if="video.isVideo == true">视频</i> 
                                    <i class="free-icon vam mr10" v-if="video.isVideo == false">文档</i> 
                                  </span>
                                  <em class="lh-menu-i-2 icon16 mr5">&nbsp;</em>{{ video.title }}
                                </a>
                              </li>

                            </ol>

                          </li>
                        </ul>
                      </menu>
                    </div>
                  </section>
                </div>
                <!-- /课程大纲 -->
              </article>
              <div>
                <h6 class="c-i-content c-infor-title">
                  <span>课程评价</span>
                </h6>
                <div>
                  <section class="course-txt-body">
                    <!-- 如果购买过该课程，则显示评论框 -->
                    <div class="edit-reply" v-if="showEditBox || Number(courseWebVo.price) === 0">
                      <div class="creator-msg">
                        <el-avatar shape="square" :size="80" :src="loginInfo.avatar"></el-avatar>
                        <span class="creator-nickname"
                          :class="[{ 'teacher_identification': Number(loginInfo.sign) === 1 }]"><b>{{ loginInfo.nickname }}</b></span>
                      </div>
                      <!-- <mavon-editor v-model="content" ref=md  @imgAdd="imgAdd" :toolbars="toolbars"/> -->
                      <el-input type="textarea" :rows="4" placeholder="请输入评论内容" v-model="content">
                      </el-input>
                      <div class="submit-area">
                        <el-button type="success" id="submit-btn" @click="commentCourse()" style="background: #00A0DC; border: #00A0DC; margin-bottom: 5px;">回复</el-button>
                      </div>
                    </div>
                    <div class="course-comment-list" v-for="comment in commentList" :key="comment.id">
                      <el-avatar shape="circle" :size="80" :src="comment.avatar"></el-avatar>
                      <div class="creator-msg">
                        <p><span class="nickname">{{ comment.nickname }}</span><span
                            class="gmt-create">{{ comment.gmtCreate }}</span></p>
                        <p class="desc">{{ comment.content }}</p>
                      </div>
                    </div>
                  </section>
                </div>
              </div>
            </div>
          </section>
        </article>
        <aside class="fl col-3">
          <div class="i-box">
            <div>
              <section class="c-infor-tabTitle c-tab-title">
                <a title href="javascript:void(0)">主讲讲师</a>
              </section>
              <section class="stud-act-list">
                <ul style="height: auto;">
                  <li>
                    <div class="u-face">
                      <a :href="'/teacher/' + courseWebVo.teacherId">
                        <img :src="courseWebVo.avatar" width="50" height="50" alt>
                      </a>
                    </div>
                    <section class="hLh30 txtOf">
                      <a class="c-333 fsize16 fl"
                        :href="'/teacher/' + courseWebVo.teacherId">{{ courseWebVo.teacherName }}</a>
                    </section>
                    <section class="hLh20 txtOf">
                      <span class="c-999">{{ courseWebVo.intro }}</span>
                    </section>
                  </li>
                </ul>
              </section>
            </div>
          </div>
        </aside>
        <div class="clear"></div>
      </div>
    </section>
    <!-- /课程详情 结束 -->
  </div>
</template>

<script>
import courseApi from '@/api/course'
import ordersApi from '@/api/order'
import forumApi from '@/api/forum'
import cookie from 'js-cookie'
import Base64 from 'js-base64'
import 'github-markdown-css/github-markdown.css' // 然后添加样式markdown-body


export default {
  //  asyncData({ params, error }) {
  //    return courseApi.getCourseInfo(params.id)
  //       .then(response => {
  //         return {
  //           courseWebVo: response.data.data.courseWebVo,
  //           chapterVideoList: response.data.data.chapterVideoList,
  //           courseId: params.id
  //         }
  //       })
  //  },
  asyncData({ params, error }) {
    return { courseId: params.id }
  },
  data() {
    return {
      courseWebVo: {},
      chapterVideoList: [],
      isbuy: false,
      showEditBox: false,
      commentList: [],
      content: '',
      loginInfo: {},
      toolbars: {
        bold: true, // 粗体
        italic: true, // 斜体
        header: true, // 标题
        underline: true, // 下划线
        strikethrough: true, // 中划线
        mark: false, // 标记
        superscript: false, // 上角标
        subscript: false, // 下角标
        quote: true, // 引用
        ol: true, // 有序列表
        ul: true, // 无序列表
        link: true, // 链接
        imagelink: true, // 图片链接
        code: true, // code
        table: true, // 表格
        fullscreen: true, // 全屏编辑
        readmodel: false, // 沉浸式阅读
        htmlcode: true, // 展示html源码
        help: false, // 帮助
        /* 1.3.5 */
        undo: true, // 上一步
        redo: false, // 下一步
        trash: false, // 清空
        save: false, // 保存（触发events中的save事件）
        /* 1.4.2 */
        navigation: false, // 导航目录
        /* 2.1.8 */
        alignleft: true, // 左对齐
        aligncenter: true, // 居中
        alignright: true, // 右对齐
        /* 2.2.1 */
        subfield: true, // 单双栏模式
        preview: true, // 预览
        boxShadow: false
      },
    }
  },
  created() {
    this.increaseViewCount(this.courseId)
    this.getInfo()
    this.initCourseInfo()
  },
  methods: {
    watchVideo(video) {
      console.log(video)
      if (!(this.isbuy) && (Number(this.courseWebVo.price) !== 0)) {
        this.$message({
          message: '请先购买课程后再观看',
          type: 'warning'
        });
        return false;
      }
      if(video.isVideo == true){
      let routeData = this.$router.resolve('/player/' + video.videoSourceId)
      window.open(routeData.href, '_blank')
      }else{
        const Base64 = require('js-base64').Base64
        console.log('Base64',Base64)
        window.open('http://8.138.0.248:8012/onlinePreview?url='+ encodeURIComponent(Base64.encode(video.documentUrl)), '_blank')
        console.log('video',video)
      }
    
    },

    increaseViewCount(courseId) {
      courseApi.increaseViewCount(courseId).then(response => {
      })
    },

    //展示评论列表
    showCommentList(courseId) {
      courseApi.commentList(courseId).then(response => {
        this.commentList = response.data.data.list
      })
    },

    //跳转到课程详细页面
    pushToDetail() {

      let routeData = this.$router.resolve('/study/' + this.courseId)
      window.open(routeData.href, '_blank')
      // this.$router.push({name: 'study', params:{courseId: this.courseId}})

    },

    //是否显示编辑框
    isShowEditBox(courseId, userId) {
      ordersApi.showEditBox(courseId, userId).then(response => {
        this.showEditBox = response.data.data.showEditBox
      })
    },

    //获取用户信息
    getInfo() {
      //从cookie获取用户信息
      var userStr = cookie.get('ucenter')
      // 把字符串转换json对象(js对象)
      if (userStr) {
        this.loginInfo = JSON.parse(userStr)
      }
    },

    commentCourse() {
      var eduComment = {}
      eduComment.courseId = this.courseWebVo.id
      eduComment.teacherId = this.courseWebVo.teacherId
      eduComment.memberId = this.loginInfo.id
      eduComment.nickname = this.loginInfo.nickname
      eduComment.avatar = this.loginInfo.avatar
      eduComment.content = this.content
      courseApi.addComment(eduComment).then(response => {
        this.showCommentList(this.courseWebVo.id)
        this.content = ''
      })
    },

    //markdown编辑器的 图片上传功能
    imgAdd(pos, $file) {
      var formdata = new FormData();
      formdata.append('image', $file);
      console.log(formdata)
      forumApi.uploadOssFile(formdata)
        .then(response => {
          this.$refs.md.$img2Url(pos, response.data.data.url)
        })

    },

    //查询课程详情信息
    initCourseInfo() {
      courseApi.getCourseInfo(this.courseId)
        .then(response => {
          this.courseWebVo = response.data.data.courseWebVo,
            this.chapterVideoList = response.data.data.chapterVideoList,
            this.isbuy = response.data.data.isBuy
          this.isShowEditBox(this.courseId, this.loginInfo.id)
          this.showCommentList(this.courseId)
        })
    },
    //生成订单
    createOrders() {
      console.log('进入订单')
      console.log(this.courseId)
      ordersApi.createOrders(this.courseId)
        .then(response => {
          console.log('调用成功')
          //获取返回订单号
          //生成订单之后，跳转订单显示页面(动态路由)
          console.log(response.data.data.orderId)
          this.$router.push({ path: '/orders/' + response.data.data.orderId })
        })
    }
  }
};
</script>
<style scoped>
.course-comment-list {
  border-bottom: 1px solid #DCDFE6;
  padding-top: 20px;
  padding-bottom: 20px;
}

.course-comment-list .creator-msg {
  width: 87%;
  display: inline-block;
  vertical-align: middle;
  margin-left: 10px;
}

.course-comment-list .nickname {
  font-size: 16px;
}

.course-comment-list .gmt-create {
  font-size: 16px;
  float: right;
}

.course-comment-list .desc {
  margin-top: 30px;
  font-size: 16px;
}

.el-avatar {
  vertical-align: middle;
}

.edit-reply {
  width: 100%;
}

.creator-msg {
  margin-bottom: 20px;
}

.submit-area {
  display: flex;
  height: 45px;
  margin-top: 20px;
  border-bottom: 1px solid #DCDFE6;
  text-align: center;
  justify-content: center;
}

#submit-btn {
  /* 右侧显示发布按钮 */
  float: right;

}
.c-attr-mt a{
  cursor: pointer
}
</style>
